import { Body, Controller, Get, Post, Res, UseInterceptors, Request, Query, HttpException, HttpStatus } from '@nestjs/common';
import { ValidTokenInterceptor } from 'src/interceptor/validToken';
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { ConcernService } from './concern.service';

import {
  apibefore
} from '../../router'
import { Concern } from './concern.entity';
import utils from 'src/common/utils';
@Controller(apibefore('/concern'))
@UseInterceptors(ValidTokenInterceptor)
@UseInterceptors(ValidRSAInterceptor)
export class ConcernController {
  constructor(private readonly service: ConcernService) {}

  @Post('/create')
  async create(@Body() data: Concern, @Request() req) {
    let concern = new Concern()
    concern.concern_wx_user_id = data.concern_wx_user_id || undefined
    concern.concern_ic_group = data.concern_ic_group || undefined
    concern.create_time = new Date()
    concern.wx_user = req.headers.wx_user
    
    let result = await this.service.create(concern)
    if(result.error){
      return new HttpException(result.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return result
    }
  }

  @Post('/create_by_video_id')
  async create_by_video_id(@Body() data, @Request() req){
    return await this.service.create_by_video_id(data.video_id, req.headers.wx_user)
  }

  @Post('/delete')
  async delete(@Body() data: {id: number, unconcern: string}, @Request() req) {
    let err = await this.service.remove(data.id, data.unconcern, req.headers.wx_user)
    if(err){
      return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return true
    }
  }

  @Get('/findAll_My')
  async findAll_My(@Query() params,@Request() req){
    return await this.service.findAll_ByWx_User(req.headers.wx_user, params.keyword)
  }

  @Get('/findAll_Works')
  async findAll_Works(@Query() params, @Request() req){
    let { skip, take } = utils.getLimit(params.pageSize, params.pageIndex)
    return await this.service.findAll_Works(skip, take, req.headers.wx_user)
  }

  @Get('/findIsUpdate')
  async findIsUpdate(@Query() params, @Request() req){
    return await this.service.findIsUpdate(req.headers.wx_user)
  }

  @Get('/findConcern_by_video_id')
  async findConcern_by_video_id(@Query() params, @Request() req){
    return await this.service.findConcern_by_video_id(params.video_id, req.headers.wx_user)
  }
}
